<template>
  <div class="xtx-form">
    <div class="user-info">
      <img :src="qqInfo?.figureurl" alt="" />
      <p>
        Hi，{{ qqInfo?.nickname }} 欢迎来小兔鲜，完成绑定后可以QQ账号一键登录哦~
      </p>
    </div>
    <div class="xtx-form-item">
      <div class="field">
        <i class="icon iconfont icon-phone"></i>
        <input
          class="input"
          type="text"
          placeholder="绑定的手机号"
          v-model="mobile"
          ref="mobileRef"
        />
      </div>
      <div class="error" v-if="mobileError">
        <i class="iconfont icon-warning" />{{ mobileError }}
      </div>
    </div>
    <div class="xtx-form-item">
      <div class="field">
        <i class="icon iconfont icon-code"></i>
        <input
          class="input"
          type="text"
          placeholder="短信验证码"
          v-model="code"
        />
        <span class="code" @click="onSend">
          {{ time === 0 ? '发送验证码' : `${time}s后发送` }}
        </span>
      </div>
      <div class="error" v-if="codeError">
        <i class="iconfont icon-warning" />{{ codeError }}
      </div>
    </div>
    <a href="javascript:;" class="submit" @click="onBind">立即绑定</a>
  </div>
</template>

<script name="CallbackBind" lang="ts" setup>
import { QQUserInfo, QQUserInfoRes } from '@/types/user'
import { ref } from 'vue'

import { useField, useForm } from 'vee-validate'
import { mobileRule, codeRule } from '@/validate/rules'
import useStore from '@/store'
import { useCountDown } from '@/utils/hooks'
import Message from '@/components/message'
import { useRouter } from 'vue-router'
const { userStore } = useStore()
const router = useRouter()

// 1.判断是否登录
const qqInfo = ref<QQUserInfo>()
let openId = ref('')
if (QC.Login.check()) {
  // 2. 获取QQ用户信息
  QC.api('get_user_info').success((res: QQUserInfoRes) => {
    qqInfo.value = res.data
  })
  // 3. 获取openId
  QC.Login.getMe((id) => {
    openId.value = id
  })
}
// 定义表单验证规则
const validationSchema = {
  mobile: mobileRule,
  code: codeRule
}
// 使用表单
const { validate } = useForm({
  validationSchema
})
// 使用字段
const {
  value: mobile,
  errorMessage: mobileError,
  validate: validateMobile
} = useField<string>('mobile')
const { value: code, errorMessage: codeError } = useField<string>('code')

// 发送验证码
const mobileRef = ref<HTMLInputElement | null>(null)
const { time, start } = useCountDown(60)
const onSend = async () => {
  if (time.value > 0) return
  // 判断手机号是否验证通过
  const res = await validateMobile()
  if (!res.valid) {
    // 验证失败 获取焦点
    mobileRef.value!.focus()
    return
  }
  // 发送请求绑定qq
  await userStore.sendQQBindMsg(mobile.value)
  // 开启倒计时
  start()
}

// 绑定
const onBind = async () => {
  const res = await validate()
  if (res.errors.mobile || res.errors.code) return
  await userStore.qqBindLogin(openId.value, mobile.value, code.value)
  Message.success('绑定成功')
  router.push('/')
}
</script>

<style scoped lang="less">
.user-info {
  width: 320px;
  height: 70px;
  margin: 0 auto;
  display: flex;
  background: #f2f2f2;
  align-items: center;
  padding: 0 10px;
  margin-bottom: 25px;
  img {
    background: #f2f2f2;
    width: 50px;
    height: 50px;
  }
  p {
    padding-left: 10px;
  }
}
.code {
  position: absolute;
  right: 0;
  top: 0;
  line-height: 50px;
  width: 80px;
  color: #999;
  &:hover {
    cursor: pointer;
  }
}
</style>
